package wef.PW.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeMark {

    /**
     * 输入时标字符串转换为Date对象
     * @param str
     * @return
     */
    public static Date MarkToDate(String str){

        String millisecond_str = str.substring(2, 4) + str.substring(0, 2);
        String minute_str = str.substring(4, 6);
        String hour_str = str.substring(6, 8);
        String day_str = str.substring(8, 10);
        String month_str = str.substring(10, 12);
        String year_str = str.substring(12, 14);
        int a = Integer.parseInt(millisecond_str, 16);
        int milliseconds = a % 1000; // 毫秒
        int seconds = a / 1000; // 秒
        int minutes = Integer.parseInt(minute_str, 16); // 分
        int hours = Integer.parseInt(hour_str, 16); // 时
        a = Integer.parseInt(day_str, 16);
        int days = a - ((a >> 5) << 5); // 日
        int months = Integer.parseInt(month_str, 16); // 月
        Calendar nowDate = Calendar.getInstance();
        int  nowYear = nowDate.get(Calendar.YEAR);
        int years = Integer.parseInt(year_str, 16); // 年
        years = nowYear / 100 * 100 + years;
        Date date = IntFormat(years, months, days, hours, minutes, seconds, milliseconds);
        return date;
    }

    /**
     * 输入年 月 日... int转换为Date对象
     * @param years
     * @param months
     * @param days
     * @param hours
     * @param minutes
     * @param seconds
     * @param milliseconds
     * @return
     */
    public static Date IntFormat(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
        String Time = Integer.toString(years) + "/" + Integer.toString(months) + "/" +
                Integer.toString(days) + " " + Integer.toString(hours) + ":" +
                Integer.toString(minutes) + ":" + Integer.toString(seconds) + ":" +
                Integer.toString(milliseconds);
        try {
            Date date = sdf.parse(Time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String getNowTimeMark(){
        SimpleDateFormat sdf = new SimpleDateFormat("ssSSSmmHHddMMyyyy");
        Date date = new Date();
        String str = sdf.format(date);
        String str1 = Integer.toHexString(Integer.parseInt(str.substring(0, 5)));
        str1 = StrUtils.strPolish(str1, 4);
        String milliseconds = str1.substring(2, 4) + str1.substring(0, 2);
        String minutes = Integer.toHexString(Integer.parseInt(str.substring(5, 7)));
        minutes = StrUtils.strPolish(minutes, 2);
        String hours = Integer.toHexString(Integer.parseInt(str.substring(7, 9)));
        hours = StrUtils.strPolish(hours, 2);
        String days = Integer.toHexString(Integer.parseInt(str.substring(9, 11)));
        days = StrUtils.strPolish(days, 2);
        String months = Integer.toHexString(Integer.parseInt(str.substring(11, 13)));
        months = StrUtils.strPolish(months, 2);
        String years = Integer.toHexString(Integer.parseInt(str.substring(15, 17)));
        years = StrUtils.strPolish(years, 2);
        String timeMark = milliseconds + minutes + hours + days + months + years;
        System.out.println(timeMark);
        return timeMark;
    }


}
